package main

import (
	"encoding/json"
	"fmt"
	test2 "sgg/结构体/test"
)

func main() {
	mon := test2.NewMon("haha")
	fmt.Println(mon.GetName())
	fmt.Println(mon.GetName())
	mon.SetName("lala")
	fmt.Println(mon.GetName())
}

type Stu struct {
	Name  string `json:"name"`
	Age   int    `json:"age"`
	ptr   *int   `json:"ptr"`
	slice []int
	map1  map[string]string
}
type StuC struct {
	Name  string
	Age   int
	ptr   *int
	slice []int
	map1  map[string]string
}

func base() {
	var stu Stu
	//未初始化的 stu: { 0 <nil> [] map[]}
	fmt.Println("未初始化的 stu:", stu)
	//引用类型要先初始化
	stu.slice = make([]int, 2)
	stu.map1 = make(map[string]string)
	stu.map1["a"] = "1"
	stu.Name = "wang"
	stu.Age = 18
	fmt.Println("初始化后的 stu:", stu)
}

func create() {
	//1.直接声明并赋值变量
	stu := Stu{
		Name:  "xys",
		Age:   19,
		slice: []int{1},
	}
	//{xys 19 <nil> [1] map[]}
	fmt.Println(stu)

	//2.先声明，再赋值
	var stu2 Stu
	stu2.Name = "xiaowang"
	stu2.Age = 20
	//{xiaowang 20 <nil> [] map[]}
	fmt.Println(stu2)

	//3.通过 new 创建
	stu3 := new(Stu)
	stu3.Age = 1
	//&{ 1 <nil> [] map[]}
	fmt.Println(stu3)
	//因为 new 返回的是一个指针类型，因此可以通过*获取对象本身
	//{ 1 <nil> [] map[]}
	fmt.Println(*stu3)

	//4.3的创建方式等同于下面的方式
	var stu4 *Stu = new(Stu)
	(*stu4).Name = "啦啦"
	//{啦啦 0 <nil> [] map[]}
	fmt.Println(*stu4)

	//5.
	var stu5 *Stu = &Stu{
		Name: "heihei",
	}
	stu5.Age = 1
	//&{heihei 1 <nil> [] map[]}
	fmt.Println(stu5)
	fmt.Println(*stu5)
}

func valueType() {
	stu := Stu{
		Name: "xiaozhang",
	}
	stu2 := stu
	// stu2Name 赋值为 xiaozhang
	fmt.Println("stu2 name:", stu2.Name)
	//修改 stu2的名字
	stu2.Name = "haha"
	//stu2 name: haha
	fmt.Println("stu2 name:", stu2.Name)
	//stu name: xiaozhang
	fmt.Println("stu name:", stu.Name)
}
func test() {
	stu := Stu{
		Name:  "xw",
		Age:   1,
		ptr:   nil,
		slice: []int{1},
	}
	stuc := StuC{}
	stuc = StuC(stu)
	fmt.Println(stuc)
}
func jsonTest() {
	stu := Stu{
		Name:  "xw",
		Age:   1,
		ptr:   nil,
		slice: []int{1},
	}
	bytes, _ := json.Marshal(stu)
	//{"name":"xw","Age":1}
	fmt.Println(string(bytes))

	target := json.Unmarshal(bytes, &Stu{})

	fmt.Println(target)
}
